package org.jboss.as.quickstarts.login.stripes;

import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.validation.SimpleError;
import org.jboss.as.quickstarts.login.User;
import org.jboss.as.quickstarts.login.UserManager;

import javax.inject.Inject;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Gregory Rushton, gregory.rushton@gmail.com
 *         Date: 9/6/11
 *         Time: 11:47 AM
 */
@UrlBinding("/Stripes/Users.action")
public class UsersActionBean extends BaseActionBean {

    @Inject
    private UserManager userManager;

    @Inject
    private transient Logger logger;

    List<User> users;

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    @DefaultHandler
    @HandlesEvent("view")
    public Resolution view() {
        try {
            setUsers(userManager.getUsers());
        } catch (Exception e) {
            logger.log(Level.ALL, "Exception getting users: " + e.getMessage());
            getContext().getValidationErrors().addGlobalError(new SimpleError("Sorry, Exception getting users. "));
        }
        return new ForwardResolution(STRIPES_VIEW_USERS_JSP);
    }

}
